package com.fei.controller;

import com.fei.bean.SingerBean;
import com.fei.bean.VideoBean;
import com.fei.service.FileService;
import com.fei.service.SingerService;
import com.fei.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Controller
public class VideoController {

    @Autowired
    VideoService videoService;
    @Autowired
    FileService fileService;
    @Autowired
    SingerService singerService;

//    用于验证视频名是否被添加过
    @RequestMapping("/ValidVideoName")
    @ResponseBody
    public String ValidVideoName(String video_name){
        int num = videoService.ValidVideoNameService(video_name);
        if(num == 0){
            return "true";
        }
        return "false";
    }

//    video管理
    @RequestMapping("/admin/toVideoList")
    public String toVideoList(Model model){
        List<VideoBean> videoList = videoService.findAllVideoService();
        model.addAttribute("videoList",videoList);
        return "videoList";
    }


//    到添加视频页面
    @RequestMapping("/admin/toVideoInsert")
    public String toVideoInsert(Model model){
        List<SingerBean> singerBeanList = videoService.selectAllSingerService();
        model.addAttribute("singerBeanList",singerBeanList);
        return "videoInsert";
    }

//    添加视频
    @RequestMapping("/admin/add/videoInsert")
    public String videoInsert(MultipartFile file1, MultipartFile file2,
                              VideoBean videoBean){
        if (videoBean.getSinger_id() == null){
            return  "redirect:/admin/toVideoList";
        }

        //data/Music/Mv/歌手名/Mv名/视频 视频路径规则
        //data/Music/Mv/歌手名/Mv名/视频封面 视频封面路径规则
        SingerBean singerBean = singerService.selectOneIdService(videoBean.getSinger_id());
        System.out.println(videoBean);

        //文件上传
        File path = fileService.ParentPath();
        String filePath = path + "//data//Music//Mv//"+singerBean.getName()+"/"+videoBean.getVideo_name();
        Boolean f1 = fileService.FileUpload(filePath,file1.getOriginalFilename(), file1);
        Boolean f2 = fileService.FileUpload(filePath,file2.getOriginalFilename(),file2);

        videoBean.setImg("/Music/Mv/"+singerBean.getName()+"/"+videoBean.getVideo_name()+"/"+file1.getOriginalFilename());
        videoBean.setVideo_path("/Music/Mv/"+singerBean.getName()+"/"+videoBean.getVideo_name()+"/"+file2.getOriginalFilename());
        //添加到数据库
        videoService.videoInsertService(videoBean);

        return  "redirect:/admin/toVideoList";
    }

    @RequestMapping("/admin/delete/deleteVideo")
    public String deleteVideo(int id){
//        通过id查询所有
        VideoBean video = videoService.selectOneIdVideoService(id);
        SingerBean singerBean = singerService.selectOneIdService(video.getSinger_id());
//        项目上一级路径
        File path = fileService.ParentPath();
//        删除对应的文件
        Boolean flag = fileService.FileDelete(new File(path + "//data//Music//Mv//"+singerBean.getName()+"/" + video.getVideo_name()));
//        删除数据库信息
        videoService.deleteVideoOneIdService(id);
        return "redirect:/admin/toVideoList";
    }

}
